const app = getApp()
var xrRequest = require('../../../vendor/hope-sdk/core/request.js');
var config = require('../../../config.js');
var xrSdk = require('../../../vendor/hope-sdk/index.js');
const util = require('../../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{
      name: "全部"
    }, {
      name: "待确认"
    }, {
      name: "已确认"
    }, {
      name: "已完成"
    },{
      name: "已评价"
    },{
      name: "已取消"
    }],
    currentTab: 0,
    pageIndex: 1,
    loadding: false,
    pullUpOn: true,
    noContent: false,
    scrollTop: 0,
    orderList:[],
    order_id:0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      currentTab: options.currentTab || 0
    },()=>{
      that.initList();
    });
  },
  initData: function () {
    this.setData({
      orderList: [],
      pageIndex: 1,
      pullUpOn: true,
      loadding: true,
      noContent: false
    })
  },
  initList: function () {
    var that = this;
    var params = that.getParams();
    xrRequest.request(app,{
      url: config.service.rescue_list,
      login: false,
      data: params,
      success: function (res) {
        that.setData({
          orderList: that.data.orderList.concat(res.data.data.data),
          pageIndex: that.data.pageIndex + 1,
          loadding: false
        })
        // 没有更多
        if (res.data.data.has_more == false &&
          that.data.orderList.length > 0) {
          that.setData({
            pullUpOn: false
          })
        }
        // 空数据
        that.setData({
          noContent: that.data.orderList.length == 0
        });
        wx.stopPullDownRefresh();
      },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    });
  },
  getParams() {
    var that = this;
    var params = {
      page: that.data.pageIndex,
      page_size: 10,
      status: that.data.currentTab
    };
    return params;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  onPullDownRefresh() {
    this.initData();
    this.initList();
  },
  onReachBottom() {
    if (!this.data.pullUpOn) return;
    this.setData({
      loadding: true
    }, () => {
      this.initList();
    })
  },
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  change(e) {
    this.setData({
      currentTab: e.detail.index
    })
    this.onPullDownRefresh();
  },
  gotomap:function(e){
    var id = e.currentTarget.dataset.id;
    this.setData({
      order_id:id
    });
    var order = this.data.orderList.find(this.getOrderItem);
    wx.navigateTo({
      url:"../map/map?data="+encodeURIComponent(JSON.stringify(order))
    });
  },
  getOrderItem:function(element){
    var that = this;
    return element.id == that.data.order_id;
  },
  evaluate:function(e){
    var id = e.currentTarget.dataset.id;
    this.setData({
      order_id:id
    });
    var order = this.data.orderList.find(this.getOrderItem);
    wx.navigateTo({
      url:"../evaluate/evaluate?data="+encodeURIComponent(JSON.stringify(order))
    });
  },
  cancel:function(e){  // 取消
    var that = this;
    var order_id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确定取消吗',
      content: '确定取消本次救援吗？',
      success(res) {
        if (res.confirm) {
          that.cancelHandler(order_id);
          console.log('用户点确定了')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });    
  },
  cancelHandler:function(rescue_id){
    var that = this;
    wx.showLoading({
      title: '请稍候...',
      mask: true
    })
    xrRequest.request(app,{
      url: config.service.cancel_rescue,
      login: true,
      data: {
        rescue_id: rescue_id,
      },
      success: function (res) {
        if (res.data.code == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          });
          that.onPullDownRefresh();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
        wx.hideLoading();
      },
      fail: function (res) {
        wx.showToast({
          title: res,
          icon: 'none',
        })
        wx.hideLoading();
       },
      authfail: function (e) {
        wx.hideLoading();
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  }
})